import Vue from 'vue'
import VueRouter from 'vue-router'
import routesConfig from './config'

Vue.use(VueRouter)


// 解决重复点击当前路由控制台报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const formatRoutes = (configs) => {
  let routes = configs.map((config) => {
    let { path } = config
    if (!path.startsWith('/') && path !== '*') {
      path = '/' + path
    }
    return {...config, path}
  })
  console.log(routes);
  return routes
}

const router = new VueRouter({
  mode: 'history',
  routes: formatRoutes(routesConfig)
})

export default router